/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fontys.time;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Elton Figaroa
 */
public class Period2 implements IPeriod{
    
    private ITime beginTijd;
    private long periode;
    
    public Period2(ITime bt, ITime et) {
        this.beginTijd = bt;
        this.periode = (long) this.beginTijd.difference(et);        
    }    
    /**
     * 
     * @return the begin time of this period
     */
    @Override
    public ITime getBeginTime() {
        return beginTijd;
    }
    /**
     * 
     * @return the end time of this period
     */
    @Override
    public ITime getEndTime() {
        return beginTijd.plus((int) this.periode);
    }
    /**
     * 
     * @return the length of this period expressed in minutes (always positive)
     */
    @Override
    public int length() {
        return (int) periode;
    }
    
    /**
     * beginTime will be the new begin time of this period
     * @param beginTime must be earlier than the current end time
     * of this period
     */
    @Override
    public void setBeginTime(ITime beginTime) {
        this.periode = periode + beginTijd.difference(beginTime);
        this.beginTijd = beginTime;
    }
    /**
     * endTime will be the new end time of this period
     * @param beginTime must be later than the current begin time
     * of this period
     */
    @Override
    public void setEndTime(ITime endTime) {
        this.periode = (long) this.beginTijd.difference(endTime);
    }
    /**
     * the begin and end time of this period will be moved up both with [minutes]
     * minutes
     * @param minutes (a negative value is allowed)
     */
    @Override
    public void move(int minutes) {
        this.beginTijd.plus(minutes);
    }
    /**
     * the end time of this period will be moved up with [minutes] minutes
     * @param minutes  minutes + length of this period must be positive  
     */
    @Override
    public void changeLengthWith(int minutes) {
        this.periode = this.periode + (long) minutes;
    }
    /**
     * 
     * @param period
     * @return true if all moments within this period are included within [period], 
     * otherwise false
     */
    @Override
    public boolean isPartOf(IPeriod period) {
        return (period.getBeginTime().difference(this.beginTijd) >= 0 || this.getEndTime().difference(period.getEndTime()) >= 0);
    }
    /**
     * 
     * @param period
     * @return if this period and [period] are consecutive or possess a
     * common intersection, then the smallest period p will be returned, 
     * for which this period and [period] are part of p, 
     * otherwise null will be returned 
     */
    @Override
    public IPeriod unionWith(IPeriod period) {
        if (period.getBeginTime().difference(this.beginTijd) >= 0) {
            if (this.getEndTime().difference(period.getEndTime()) >= 0) {
                try {
                    return new Period(this.beginTijd, this.getEndTime());
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            } else {
                try {
                    return new Period(this.beginTijd, period.getEndTime());
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
        if (period.getBeginTime().difference(this.beginTijd) < 0) {
            if (this.getEndTime().difference(period.getEndTime()) >= 0) {
                try {
                    return new Period(period.getBeginTime(), this.getEndTime());
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            } else {
                try {
                    return new Period(period.getBeginTime(), period.getEndTime());
                } catch (Exception ex) {                    
                    System.out.println(ex.getMessage());
                 
                }
            }
        }
        return null;
    }
    /**
     * 
     * @param period
     * @return the largest period which is part of this period 
     * and [period] will be returned; if the intersection is empty null will 
     * be returned
     */
    @Override
    public IPeriod intersectionWith(IPeriod period) {
        if (period.getBeginTime().difference(this.beginTijd) >= 0) {
            if (this.getEndTime().difference(period.getEndTime()) >= 0) {
                try {
                    return new Period(period.getBeginTime(), period.getEndTime());
                } catch (Exception ex) { 
                    System.out.println(ex.getMessage());
                }
            } else {
                try {
                    return new Period(period.getBeginTime(), this.getEndTime());
                } catch (Exception ex) {               
                    System.out.println(ex.getMessage());
                }
            }
        }
        if (period.getBeginTime().difference(this.beginTijd) < 0) {
            if (this.getEndTime().difference(period.getEndTime()) >= 0) {
                try {
                    return new Period(this.beginTijd, period.getEndTime());
                } catch (Exception ex) {       
                    System.out.println(ex.getMessage());
                }
            } else {
                try {
                    return new Period(this.beginTijd, this.getEndTime());
                } catch (Exception ex) {                  
                    System.out.println(ex.getMessage());
                }
            }

        }
        return null;
    }
}
